package com.wanxs.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wanxs.mall.common.service.impl.CrudServiceImpl;
import com.wanxs.mall.product.dao.AttrAttrgroupRelationDao;
import com.wanxs.mall.product.dto.AttrAttrgroupRelationDTO;
import com.wanxs.mall.product.entity.AttrAttrgroupRelationEntity;
import com.wanxs.mall.product.service.AttrAttrgroupRelationService;
import com.wanxs.mall.product.vo.AttrGroupRelationVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 属性&属性分组关联
 *
 * @author wanxs 130001934@qq.com
 * @since 1.0.0 2023-09-01
 */
@Service
public class AttrAttrgroupRelationServiceImpl extends CrudServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity, AttrAttrgroupRelationDTO> implements AttrAttrgroupRelationService {

    @Override
    public QueryWrapper<AttrAttrgroupRelationEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<AttrAttrgroupRelationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public List<AttrAttrgroupRelationEntity> queryAttryGroupRelation(Long attrgroupId) {
        return baseDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrgroupId));
    }

    @Override
    public AttrAttrgroupRelationEntity selectOne(Long attrId) {
        return baseDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrId));
    }

    @Override
    public void deleteByAttrId(Long attrId) {
        baseDao.delete(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrId));
    }

    @Override
    public void removeBatchRelation(List<AttrAttrgroupRelationEntity> entityList) {
        baseDao.removeBatchRelation(entityList);
    }

    @Override
    public void saveBatch(List<AttrGroupRelationVO> attrGroupRelationVOS) {
        List<AttrAttrgroupRelationEntity> collect = attrGroupRelationVOS.stream().map(item -> {
            AttrAttrgroupRelationEntity entity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(item, entity);
            return entity;
        }).collect(Collectors.toList());
        this.insertBatch(collect);
    }


}